import torch
import torchvision
import torch.nn as nn
from torch.utils.data import DataLoader
#创建数据集及加载数据集
from torch.utils.tensorboard import SummaryWriter

data_set = torchvision.datasets.CIFAR10("./dataset",train=True,transform=torchvision.transforms.ToTensor(),download=True)
data_loader = DataLoader(data_set,64)


#自定义模型
class MyModule(nn.Module):
    def __init__(self):
        super(MyModule, self).__init__()
        self.conv1 = torch.nn.Conv2d(in_channels=3,out_channels=6,kernel_size=3)

    def forward(self,x):
        x = self.conv1(x)
        return x


#创建模型对象
mymodule = MyModule()
writer = SummaryWriter("conv2d_logs")
#获取经过卷积后的图像的结果
step = 0
for data in data_loader:
    imgs,target = data
    img_conv = mymodule(imgs)
    writer.add_images("input",imgs,step)
    img_conv = torch.reshape(img_conv,[-1,3,30,30])#需要进行通道数的图像格式的转换，因为add_images要求的是三个通道的
    writer.add_images("output",img_conv,step)
    step = step+1

writer.close()